//
//  TourTableViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/3.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "TourTableViewController.h"
#import "TourModel.h"
#import "TourViewLayout.h"
#import "TourTableViewCell.h"
#import "TourDetailViewController.h"

@interface TourTableViewController ()<UISearchBarDelegate>

{
    NSInteger _pageIndex;
    NSString *_searchString;
    UIImageView *_noDataImage;
    BOOL _searchBool;
}

@property (nonatomic,strong)NSMutableArray *TourMutableArray;

@property (nonatomic,strong)UISearchBar *searchBar;

@end

@implementation TourTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.tableFooterView = [[UIView alloc] init];
    _pageIndex = 1;
    _searchString = @"杭州";
    self.tableView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadTourMoreResource)];
    [self _initTourSource];
    [self showHudInView:self.view hint:@"正在加载中..."];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)loadTourMoreResource {
    _pageIndex ++;
    _searchBool = NO;
    [self _initTourSource];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"%@",searchBar.text);
    [UIView animateWithDuration:0.3 animations:^{
        [searchBar resignFirstResponder];
    }];
    [self showHudInView:self.view hint:@"正在搜索中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.searchBar endEditing:YES];
        [self.TourMutableArray removeAllObjects];
        _searchBool = YES;
        _searchString = searchBar.text;
        _pageIndex = 1;
        [self.TourMutableArray removeAllObjects];
        [self _initTourSource];
    });
}

- (void)_initTourSource {
    
    NSDictionary *diction = @{
                              @"page":[NSString stringWithFormat:@"%ld",_pageIndex],
                              @"keyword":_searchString
                              };
    NSLog(@"%@",diction);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:diction AndMenthodName:@"http://test.suntao-develop.com/api/travel/shangjia_list" completeBlock:^(id object) {
        NSLog(@"%@",object);
        [self.tableView.mj_footer endRefreshing];
        [self hideHud];
        [_noDataImage removeFromSuperview];
            NSArray *array = [object objectForKey:@"data"];
        
        if (array.count) {
            for (NSDictionary *dict in array) {
                TourModel *model = [[TourModel alloc] initWithDictionary:dict error:nil];
                TourViewLayout *layout = [[TourViewLayout alloc] _initLayoutWithTourModel:model];
                [self.TourMutableArray addObject:layout];
            }
            
            [self.tableView reloadData];
        } else {
            
            [self showHint:@"该地区暂无更多景点"];
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
            if (_searchBool) {
                _searchBool = NO;
                [self.tableView reloadData];
                _noDataImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                self.tableView.backgroundColor = BACKGROUND_COLOR;
                [self.view addSubview:_noDataImage];
                
                [_noDataImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];

            }
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.searchBar resignFirstResponder];
    
    NSLog(@"%@",((TourViewLayout *)_TourMutableArray[indexPath.row]).replaceID);
    TourDetailViewController *detail = [[TourDetailViewController alloc] init];
    detail.replaceID = ((TourViewLayout *)_TourMutableArray[indexPath.row]).replaceID;
    detail.title = @"景点商家";
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detail];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.TourMutableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tour"];
    if (!cell) {
        cell = [[TourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tour"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setLayout:self.TourMutableArray[indexPath.row]];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    return
    TourHeaderView *headerView = [[TourHeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 44)];
    self.searchBar = headerView.searchTextField;
    self.searchBar.delegate = self;
    return headerView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ((TourViewLayout *)_TourMutableArray[indexPath.row]).height;
}


- (NSMutableArray *)TourMutableArray {
    if (!_TourMutableArray) {
        _TourMutableArray = [NSMutableArray array];
    }
    return _TourMutableArray;
}

@end
